﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using OpenLS.Drawing;
using OpenLS.Spreadsheet.Formats.Biff.OfficeArt;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class MsoDrawingGroupRecord : WorkbookLevelRecord
    {
        private OfficeArtDggContainer rgChildRec;

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.MsoDrawingGroup; }
        }
       
        private byte[] _buffer;
        private bool isContinue;


        public MsoDrawingGroupRecord(byte [] buffer)
        {
            _buffer = buffer;
        }

        public MsoDrawingGroupRecord()
        {
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            _buffer = context.ReadBytes(length);
        }


        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            context.Write(_buffer);
        }

        public override void Apply(BiffReaderWorkbookContext context)
        {
            if (isContinue)
                return;
            context.DrawingGroup = this;
        }
        

        public override void ReadWithContinue()
        {
            if (isContinue)
                return;
            List<byte> _bytes = new List<byte>(_buffer);
            bool needsMoreMemory = false;
            using (MemoryStream memoryStream = new MemoryStream(_bytes.ToArray()))
            {
                using (BinaryReader reader = new BinaryReader(memoryStream))
                {
                    OfficeArtRecordHeader artRecordHeader = new OfficeArtRecordHeader();
                    artRecordHeader.Read(reader);
                    needsMoreMemory = artRecordHeader.recLen > _bytes.Count - OfficeArtRecordHeader.ByteLength;
                }
            }
            BiffRecord currentRecord = this;
            if (needsMoreMemory && Next is MsoDrawingGroupRecord)
            {
                MsoDrawingGroupRecord group = (MsoDrawingGroupRecord) Next;
                _bytes.AddRange(group._buffer);
                group.isContinue = true;
                currentRecord = Next;
            }
            ContinueRecord continueRecord = currentRecord.Next as ContinueRecord;
            
            while (continueRecord != null)
            {
                _bytes.AddRange(continueRecord.Buffer);
                continueRecord = continueRecord.Next as ContinueRecord;
            }
            using (MemoryStream memoryStream = new MemoryStream(_bytes.ToArray()))
            {
                using (BinaryReader reader = new BinaryReader(memoryStream))
                {
                    OfficeArtReadContext ctx = new OfficeArtReadContext(reader, new Dictionary<int, ObjRecord>(), new Dictionary<int, TxORecord>());
                    rgChildRec = new OfficeArtDggContainer();
                    rgChildRec.Read(ctx);
                }
            }
        }
        public OfficeArtDggContainer ChildRecord
        {
            get
            {
                return rgChildRec;
            }
        }
    }
}